import { Module } from 'vuex';
import { RootState } from './../index';
import { formDataModel, buildItemModel } from '@/views/products/selectByTag/selectByTag.module';
import { SaveDataModel } from '@/components/TopSelect/topSelecter.module.d';
import { TreeChildModel } from '@/components/CityAreaSelecter/city.module';
import { CphTableItemModel } from '@/views/products/brandBreakout/breakout.module';

interface Tag {
    [key: string]: buildItemModel[]
}
export interface AppState {
    title: string,
    keepAliveRouteNameList: string[],
    cityList: object[],
    currentViewCityList: object[],
    currentCity: object,
    searchCity: object[],
    selectByTag: formDataModel,
    selectByJd: object,
    selectByLbs: SaveDataModel,
    buildTag: Tag,
    home: {
        selectCity: TreeChildModel,
        projectId: string,
        optimalMediaType: number,
        cphTableList: CphTableItemModel[]
    },
    showBackHomeBtn: boolean
    productCode: string
}

function getDefaultState(): AppState {
    return {
        // ... 业务系统的 state 写在下方
        title: '',
        keepAliveRouteNameList: [],
        cityList: [],
        currentViewCityList: [],
        currentCity: {},
        searchCity: [],
        selectByTag: {
            city: [],
            cityCodeList: [],
            preference: [],
            preferenceIdList: [],
            customTag: [],
            isNationwide: false,
            customKeyTag: {},
            checkedCityData: [],
            checkedPreferenceData: [],
            currentCheckObjTag: []
        },
        selectByJd: {},
        selectByLbs: {
            city: {},
            areaList: [],
            tagBulidArr: {
                mediaTypes: [],
            },
            addresses: [],
        },
        buildTag: {},
        home: {
            selectCity: {
                id: '',
                name: ''
            },
            projectId: '',
            optimalMediaType: 0,
            cphTableList: []
        },
        showBackHomeBtn: true,
        productCode: ''
    };
}

const state: AppState = getDefaultState();
const mutations = {
    setKeepAliveRouteNameList: (state: AppState, keepAliveRouteNameList: string[]) => {
        state.keepAliveRouteNameList = keepAliveRouteNameList;
    },
    setCityList: (state: AppState, cityList: object[]) => {
        state.cityList = cityList;
    },
    setSearchCity: (state: AppState, searchCity: object[]) => {
        if (searchCity) {
            state.searchCity = [...state.searchCity, searchCity];
        } else {
            state.searchCity = [];
        }
    },
    setCurrentCity: (state: AppState, currentCity: object) => {
        state.currentCity = currentCity;
    },
    setViewCityList: (state: AppState, checkCity: object) => {
        state.currentViewCityList = [checkCity, ...state.currentViewCityList].slice(0, 3);
    },
    setSelectByTag: (state: AppState, selectByTag: formDataModel) => {
        state.selectByTag = selectByTag;
    },
    setSelectByJd: (state: AppState, selectByJd: object) => {
        state.selectByJd = selectByJd;
    },
    setSelectByLbs: (state: AppState, selectByLbs: SaveDataModel) => {
        state.selectByLbs = selectByLbs;
    },
    setBuildTag: (state: AppState, buildTag: Tag) => {
        state.buildTag = buildTag;
    },
    setOptimalMediaType: (state: AppState, type: number) => {
        state.home.optimalMediaType = type;
    },
    setHomeSelectCity: (state: AppState, city: TreeChildModel) => {
        state.home.selectCity = city;
    },
    setHomeProjectId: (state: AppState, id: string) => {
        state.home.projectId = id;
    },
    setHomeCphTableList: (state: AppState, data: CphTableItemModel[]) => {
        state.home.cphTableList = data;
    },
    setShowBackHomeBtn: (state: AppState, data:boolean) => {
        state.showBackHomeBtn = data;
    },
    setProductCode: (state: AppState, data:string) => {
        state.productCode = data;
    }
};
const actions = {};
const getters = {};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters,
} as Module<AppState, RootState>;
